/**
 * 
 */
package com.java.test.platmanage.mapper;

import com.java.test.platmanage.dto.GameListInfo;
import com.java.test.util.PageInfo;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 游戏列表db操作
 *
 */
@Component
public class GameListMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<GameListInfo> selectgameList(GameListInfo info, PageInfo<GameListInfo> page) {
		String sql = "select a.*,b.gname from plat_game_info a left join sys_game_type b on a.game_type=b.id where 1=1 ";
		List<Object> param = new ArrayList<>();
		if (info.getStatus() != -1) {
			sql += " and a.status = ?";
			param.add(info.getStatus());
		}

		if (info.getGameName() != null && !"".equals(info.getGameName())) {
			sql += " and a.game_name like ?";
			param.add("%" + info.getGameName() + "%");
		}
		if (info.getGameType() != 0) {
			sql += " and a.game_type=?";
			param.add(info.getGameType());
		}
		if (info.getPlatId() != -1) {
			sql += " and a.plat_id=?";
			param.add(info.getPlatId());
		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by sort desc limit ?,?";
		param.add(start);
		param.add(limit);
		Object[] array = param.toArray(new Object[param.size()]);

		List<GameListInfo> list = jdbcTemplate.query(sql, new GameListInfo(), array);

		return list;
	}

	public int selectgameListcount(GameListInfo info) {
		String sql = "select count(*) from plat_game_info a left join sys_game_type b on a.game_type=b.id where 1=1 ";

		List<Object> param = new ArrayList<>();
		if (info.getStatus() != -1) {
			sql += " and a.status = ?";
			param.add(info.getStatus());
		}

		if (info.getGameName() != null && !"".equals(info.getGameName())) {
			sql += " and a.game_name like ?";
			param.add("%" + info.getGameName() + "%");
		}
		if (info.getGameType() != 0) {
			sql += " and a.game_type=?";
			param.add(info.getGameType());
		}
		if (info.getPlatId() != -1) {
			sql += " and a.plat_id=?";
			param.add(info.getPlatId());
		}

		Object[] array = param.toArray(new Object[param.size()]);
		int i = jdbcTemplate.queryForObject(sql, Integer.class, array);

		return i;
	}

	public GameListInfo selectgamebyId(int id) {
		String sql = "select a.*,b.gname from plat_game_info a left join sys_game_type b on a.game_type=b.id where a.id =?";

		GameListInfo info = jdbcTemplate.queryForObject(sql, new GameListInfo(), id);

		return info;
	}

	public int updateGameListInfo(GameListInfo info) {

		String sql = "update plat_game_info set game_name=?,game_code=?,fictitious_number=?,area_id=?,area_name=?,"
				+ "status=?,content=?,game_type=?,fictitious_number_max=?,found_type=?,isnottiger_hot=? where id=?";

		int i = jdbcTemplate.update(sql, info.getGameName(), info.getGameCode(), info.getFictitiousNumber(),
				info.getAreaId(), info.getAreaName(), info.getStatus(), info.getContent(), info.getGameType(),
				info.getFictitiousNumberMax(), info.getFoundTypeId(), info.getIsNotTigerHot(), info.getId());

		return i;
	}

	public int updateGamesortInfo(List<GameListInfo> info) {
		String sql = "update plat_game_info set sort=? where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return info.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, info.get(i).getSort());
				ps.setInt(2, info.get(i).getId());
			}
		});

		return 1;
	}

	public int insertGameInfo(GameListInfo info) {
		String sql = "insert into plat_game_info(game_name,game_code,fictitious_number,area_id,area_name"
				+ ",open_language,status,content,plat_id,plat_name,sort,game_type,found_type,found_name)values"
				+ "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		int i = jdbcTemplate.update(sql, info.getGameName(), info.getGameCode(), info.getFictitiousNumber(),
				info.getAreaId(), info.getAreaName(), info.getOpenLanguage(), info.getStatus(), info.getContent(),
				info.getPlatId(), info.getPlatName(), info.getSort(), info.getGameType(), info.getFoundTypeId(),
				info.getFoundName());

		return i;

	}

	/**
	 * @param info
	 * @return
	 */
	public int updategamestatusInfo(GameListInfo info) {
		String sql = "update plat_game_info set status=?,isnot_hot=? where id=?";

		int i = jdbcTemplate.update(sql, info.getStatus(), info.getIsNotHot(), info.getId());

		return i;
	}

	public int updategamestatusInfoPL(final int[] ids, int status) {
		String sql = "update plat_game_info set status=" + status + " where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	public int updategameisnothotInfoPL(final int[] ids, int isnotHot) {
		String sql = "update plat_game_info set isnot_hot=" + isnotHot + " where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	/**
	 * @param gametypeid
	 * @return
	 */
	public List<GameListInfo> selectgamebygametype(int gametypeid) {
		String sql = "select a.*,b.gname from plat_game_info a left join sys_game_type b on a.game_type=b.id where a.game_type =?";

		List<GameListInfo> info = jdbcTemplate.query(sql, new GameListInfo(), gametypeid);

		return info;
	}

	/**
	 * 根据游戏编号和游戏id
	 * 
	 * @param gameNo
	 * @author
	 * @date 2020/9/26 0026 9:29
	 * @return java.util.List<com.java.test.platmanage.dto.GameListInfo>
	 */
	public List<GameListInfo> selectGameByGameNo(String gameNo) {
		StringBuffer sql = new StringBuffer(
				"SELECT a.*,'' as gname FROM plat_game_info a WHERE a.game_code = ? or a.id = ?");

		List<GameListInfo> info = jdbcTemplate.query(sql.toString(), new GameListInfo(), gameNo, gameNo);

		return info;
	}

	/**
	 * 根据平台id
	 * 
	 * @param platId
	 * @author
	 * @date 2020/9/26 0026 9:28
	 * @return java.util.List<com.java.test.platmanage.dto.GameListInfo>
	 */
	public List<GameListInfo> selectGameByPlatId(String platId) {
		StringBuffer sql = new StringBuffer("SELECT a.*,'' as gname FROM plat_game_info a WHERE a.plat_id = ? ");

		List<GameListInfo> info = jdbcTemplate.query(sql.toString(), new GameListInfo(), platId);

		return info;
	}

	/**
	 * @return
	 */
	public List<String> selectallGame() {

		String sql = "select game_code from plat_game_info";

		List<String> info = jdbcTemplate.queryForList(sql, String.class);

		return info;
	}
}
